package com.stenio.fancat.service.baby;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.baby.BabyIconImage;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.form.user.UserRelationshipForm;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;


/**
 * baby头像
 */
@Service
public class BabyIconImageService extends ServiceImpl<BabyIconImageMapper, BabyIconImage> {

    @Autowired
    private UserRelationshipMapper userRelationshipMapper;

    @Autowired
    private BabyIconImageMapper babyIconImageMapper;

    @Autowired
    private OssService ossService;

    @Value("${oss.piggybank.endpoint}")
    private String piggybankEndPoint;


    /**
     * 保存图片到OSS服务器--->数据库
     * @param file
     * @return
     */
    public BabyIconImage insertBabyIconImageByCondition(MultipartFile file, String userId){
        if (null == userId && null == file) {
            throw new RuntimeException("参数不能为空");
        }

        BabyIconImage image = new BabyIconImage();
        Date date = new Date();
        image.setCreateTime(date);
        image.setUpdateTime(date);
        UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(userId);
        image.setBabyId(userRelationship.getBabyId());
        try {
            String fileName = ossService.getIconName(file.getOriginalFilename(),userRelationship.getBabyId());//用babyid当做图片的名字
            image.setImage("https://piggybank-photos.oss-cn-beijing.aliyuncs.com/"+"babyIcon/"+fileName);
            ossService.putObjectSecond("babyIcon/"+fileName,file.getBytes());
            babyIconImageMapper.insert(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 通过userId修改头像图片
     * @param file
     * @param userId
     * @return
     */
    public BabyIconImage modifyBabyIconImageByCondition(MultipartFile file,String userId){
        if (null == userId && null == file) {
            throw new RuntimeException("参数不能为空");
        }

        // 通过用户ID和图片url修改
        BabyIconImage image = new BabyIconImage();
        UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(userId);
        image.setBabyId(userRelationship.getBabyId());
        BabyIconImage iconImage = babyIconImageMapper.selectByBid(image.getBabyId());
        //先删除OSS服务器上的文件
        int index = iconImage.getImage().lastIndexOf("/");
        String iconName = iconImage.getImage().substring(index+1);
        System.out.println(iconName);//控制台查看截取文件的名字
        //ossService.deleteFile(iconName);
        //删除数据库原有头像的数据
        babyIconImageMapper.deleteByBid(image.getBabyId());
        //调用上传头像的方法
        insertBabyIconImageByCondition(file,userId);

        return iconImage;
    }

    /**
     * 通过用户ID获取baby图片信息
     *
     * @param userRelationshipForm
     * @return
     */
    public BabyIconImage queryBabyIconImageByCondition(UserRelationshipForm userRelationshipForm) {
        if (null == userRelationshipForm) {
            throw new RuntimeException("参数不能为空");
        }
        // 通过用户ID查询
        BabyIconImage entity = new BabyIconImage();
        entity.setBabyId(userRelationshipForm.getBabyId());
        babyIconImageMapper.selectOne(entity);
        return babyIconImageMapper.selectOne(entity);
    }

}
